package com.cloudptt.wp;

import java.util.Stack;

import com.cloudptt.api.product.log.Log;

import android.app.Activity;

public class ScreenManager 
{
    private static Stack<Activity> activityStack;
    private static ScreenManager instance;
    private ScreenManager() {
       
    }

    public static ScreenManager getScreenManager() {
        if (instance == null) {
            instance = new ScreenManager();
        }
        return instance;
    }

 // 将当前Activity推入栈中
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
        Log.d("lq", "pushActivity:"+ activity.getClass().getName());
    }
    
    // 退出栈顶Activity
    public void popActivity(Activity activity) {
        if (activity != null) {
        	Log.d("lq", "popActivity:"+ activity.getClass().getName());
            activity.finish();
            activityStack.remove(activity);
            activity = null;
        }
    }
    
 // 退出栈顶Activity
    public void popActivity() {
    	 Activity activity = currentActivity();
         if (activity != null) 
         {
        	 Log.d("lq", "popActivity activity name = "+activity.getClass());
        	 popActivity(activity);;
         }
    }

    // 获得当前栈顶Activity
    public Activity currentActivity() {
        Activity activity = null;
        if(!activityStack.isEmpty())
        {
        	activity = activityStack.lastElement();
        }
        return activity;
    }

    

    // 退出栈中所有Activity
    public void popAllActivity() {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            popActivity(activity);
        }
    }
    
 // 退出栈中所有Activity
    public void popAllActivityExceptOne(Class<?> cls) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                break;
            }
            
            Log.d("lq","popAllActivityExceptOne activity = "+activity.getClass());
            popActivity(activity);
        }
    }
}
